<template>
	<!-- 本页面是为了测试是否可以用wx-open-launch-weapp标签；实现免授权去小程序 -->
	<wx-open-launch-weapp
	    id="launch-btn"
		ref ="launchBtn"
	    :username="originalId"
	    :path="path"
		:appid="wxAppid"
	  >
	    <script type="text/wxtag-template">
	      <button class="launch-btn">点击跳转</button>
	    </script>
	  </wx-open-launch-weapp>
</template>
<script setup>
	import { doLogin } from '@/utils/common.js'
	import { useglobal } from '@/stores'
	
	const launchBtn = ref(null);
	const globalData = useglobal();
	const originalId = ref('gh_a2b49601ae48')//小程序原始id
	const wxAppid = ref('wx2305b364aa5ca2cf')//小程序的appid
	const path = ref('pages/login/getPhone.html')
	var TIMESTAMP = Math.floor(Date.now() / 1000);// 生成时间戳
	var NONCE_STR = Math.random().toString(36).substr(2, 15);// 生成随机字符串(nonceStr)
	onLoad(() => {
		let url = window.location.search;
		const code = new URLSearchParams(url).get('code');
		doLogin(code).then((res) => {
			console.log('所有的数据',res);
			const videoKey = uni.getStorageSync('videoKey')
			if(res.phone){//暂时取返
				uni.navigateTo({
					url:'/pages/video/video?key='+ videoKey
				})
			}else{//没有手机号获取手机号
				// 对 query 进行编码
				const token = uni.getStorageSync('third_session');
				const query = `token=${token}`; 
				const encodedQuery = encodeURIComponent(query);
				// 尝试打开小程序
				//configWxJsSdk();
				
				//launchBtn.value.click();
				
				// const miniProgramUrl =
				// 	`weixin://dl/business/?appid=wx2305b364aa5ca2cf&path=pages/login/getPhone&query=${encodedQuery}&env_version=trial`
				
				// const interval = setInterval(() => {
				//     const data = localStorage.getItem(callbackKey);
				//     if (data) {
				//       console.log("小程序返回数据：", JSON.parse(data));
				//       localStorage.removeItem(callbackKey); // 清理
				//       clearInterval(interval);
				//     }
				//   }, 500);
				
			}
			
		}).catch(err => {
			console.error('流程出错:', err);
		});
	})
	
	
	const configWxJsSdk = () =>{
		console.log(globalData.appid,TIMESTAMP,NONCE_STR,globalData.signature);
		
		jWeixin.config({
			debug: true,
			appId: globalData.appid,
			timestamp: TIMESTAMP,
			nonceStr: NONCE_STR,
			signature: globalData.signature,
			openTagList: ['wx-open-launch-weapp'],
			success: () => {
				console.log('5微信 JS-SDK 配置成功');
			},
			fail: (err) => {
				console.error('4微信 JS-SDK 配置失败:', err);
			}
		})
		
		
		jWeixin.ready(() => {
			console.log('2微信JSSDK配置完成')
		})
		
		jWeixin.error((err) => {
			console.error('1微信JSSDK配置失败:', err)
			uni.showToast({
				title: '3微信配置失败',
				icon: 'none'
			})
		})
		
		
		jWeixin.checkJsApi({
		  jsApiList: ['chooseImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
		  success: function(res) {
			console.log('正常使用',res);
		  }
		});
		
		
		
	}
	
</script>

<style lang="scss" scoped>

</style>